import 'package:flutter/foundation.dart';
import '../config/env_config.dart';

/// 日志工具类，用于统一管理应用日志
class Logger {
  static final Logger _instance = Logger._internal();
  
  // 是否启用日志
  final bool _enabled = EnvConfig.currentEnv != EnvType.prod;
  
  // 单例模式
  factory Logger() => _instance;
  
  Logger._internal();
  
  /// 打印信息日志
  void info(String message) {
    if (_enabled) {
      debugPrint('ℹ️ INFO: $message');
    }
  }
  
  /// 打印调试日志
  void debug(String message) {
    if (_enabled) {
      debugPrint('🔍 DEBUG: $message');
    }
  }
  
  /// 打印警告日志
  void warning(String message) {
    if (_enabled) {
      debugPrint('⚠️ WARNING: $message');
    }
  }
  
  /// 打印错误日志
  void error(String message, {Object? exception, StackTrace? stackTrace}) {
    if (_enabled) {
      final sb = StringBuffer();
      sb.writeln('❌ ERROR: $message');
      
      if (exception != null) {
        sb.writeln('Exception: $exception');
      }
      
      if (stackTrace != null) {
        sb.writeln('StackTrace: $stackTrace');
      }
      
      debugPrint(sb.toString());
    }
  }
  
  /// 打印API请求日志
  void api(String method, String url, {int? statusCode, int? duration, String? error}) {
    if (_enabled) {
      final sb = StringBuffer();
      final emoji = error != null ? '❌' : '✅';
      
      sb.write('$emoji API $method $url');
      
      if (statusCode != null) {
        sb.write(' | 状态码: $statusCode');
      }
      
      if (duration != null) {
        sb.write(' | 耗时: ${duration}ms');
      }
      
      if (error != null) {
        sb.write(' | 错误: $error');
      }
      
      debugPrint(sb.toString());
    }
  }
}